package com.icart.controller.personInterceptor;

import com.icart.context.HttpUserContext;
import com.icart.context.provider.UserContextProvider;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;
import com.icart.entities.CuCustinfoEntity;
import com.icart.repositories.CustinfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserInterceptor implements HandlerInterceptor {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private CustinfoRepository custinfoRepository;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        ContextStorage storage2=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage2.getUserContext("loginuser");
        if(userContext==null){
            response.sendRedirect("/person/login");
            return false;
        }
        else{
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
